import 'package:flutter/material.dart';
import 'package:kasax_demos2/commons/common_test_datas.dart';
import 'package:kasax_demos2/meiyingfilm/page_film_details.dart';
import 'package:kasax_demos2/widgets/image_widget.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'entry/film_response.dart';
import 'meiy_widgets.dart';

/// 电影列表条目
class FilmItemWidget extends StatefulWidget {
  final Subjects subjectInf;

  const FilmItemWidget(this.subjectInf, {Key? key}) : super(key: key);

  @override
  _FilmItemWidgetState createState() => _FilmItemWidgetState();
}

class _FilmItemWidgetState extends State<FilmItemWidget> {
  @override
  Widget build(BuildContext context) {
    if (widget.subjectInf == null) {
      return Container();
    }
    return GestureDetector(
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => PageMovieDetails(widget.subjectInf)));
      },
      child: Container(
        height: 200,
        child: Row(
          children: [
            SizedBox(
              width: 130,
              height: 180,
              child: CachedNetworkImage(
                fit: BoxFit.cover,
                placeholder: (context, url) => Container(
                  color: Color(0xFFD6D6D6),
                  child: Center(
                    child: SizedBox(
                      width: 30,
                      height: 30,
                      child: CircularProgressIndicator(
                        color: Colors.blueGrey,
                      ),
                    ),
                  ),
                ),
                errorWidget: (context, url, error) =>
                    // Image.asset("assets/images/img_placeholder.jpeg"),
                    Image.network(TestDatas.IMGS[0]),
                imageUrl: widget.subjectInf.images!.small!,
              ),
            ),
            Expanded(
                child: Container(
              margin: EdgeInsets.only(left: 10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  Text("电影名称:${widget.subjectInf.title}"),
                  Text("上映年份:${widget.subjectInf.year}"),
                  Text("电影类型:${widget.subjectInf.genres!.join(',')}"),
                  Text("豆瓣评分:${widget.subjectInf.rating!.average}分"),
                  Row(
                    children: [
                      Text("主要演员"),
                      widget.subjectInf.casts == null
                          ? Container()
                          : Row(
                              children: widget.subjectInf.casts!.map((e) {
                                return e == null ||
                                        e.avatars == null ||
                                        e.avatars!.medium == null
                                    ? Container()
                                    : clipCirclePosterIcon(e);
                              }).toList(),
                            )
                    ],
                  )
                ],
              ),
            ))
          ],
        ),
      ),
    );
  }

  /// 演职人员头像
  Widget clipCirclePosterIcon(Casts e) {
    return Container(
      margin: EdgeInsets.only(right: 5),
      child: PerformerIconWidet("${e.avatars!.medium}"),
    );
  }

  Container buildPerformerIcons(Casts e) {
    return Container(
      margin: EdgeInsets.only(left: 5),
      child: CircleAvatar(
        radius: 18,
        backgroundImage: NetworkImage(
          "${e.avatars!.medium}",
        ),
      ),
    );
  }
}
